package com.wlyuan.open.scanner.domain.contract;

import com.wlyuan.open.scanner.dao.entity.ContractIndexDO;
import com.wlyuan.open.scanner.domain.attach.Attach;
import com.wlyuan.open.scanner.domain.order.OrderAttach;
import com.wlyuan.open.scanner.utils.JsonUtils;
import com.wlyuan.open.scanner.utils.mappings.BaseMapping;
import com.wlyuan.open.scanner.utils.mappings.MappingInvalid;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author lijing
 * @description
 */
public class ContractConvert {

    private static final Convert CONVERT = Mappers.getMapper(Convert.class);

    @Mapper(uses = MappingInvalid.class)
    public interface Convert {

        @Mappings({
                @Mapping(target = "id", source = "indexId"),
                @Mapping(target = "tenantId", source = "indexPartacompanyid"),
                @Mapping(target = "sourceKey", source = "indexId"),
                @Mapping(target = "createTime", source = "indexCreatetime"),
                @Mapping(target = "createBy", source = "indexCreator"),

                @Mapping(target = "code", source = "indexCode"),
                @Mapping(target = "name", source = "indexName"),
                @Mapping(target = "content", source = "indexContent"),
                @Mapping(target = "startDate", source = "indexFromtime"),
                @Mapping(target = "endDate", source = "indexTotime"),
                @Mapping(target = "status", source = "indexStatus"),
                @Mapping(target = "statusUpdateAt", source = "indexStatustime"),
                @Mapping(target = "comments", source = "indexComments"),
                @Mapping(target = "type", source = "indexPartbtype"),
                @Mapping(target = "url", source = "indexContracturl"),
                @Mapping(target = "evidenceUrl", source = "indexContractsigneurl"),
                @Mapping(target = "signature", source = "indexEsignature"),

                @Mapping(target = "first.tenantId", source = "indexPartacompanyid"),
                @Mapping(target = "first.name", source = "indexPartacompanyname"),
                @Mapping(target = "first.address", source = "indexPartaaddr"),
                @Mapping(target = "first.contactName", source = "indexPartacontact"),
                @Mapping(target = "first.contactMobile", source = "indexPartaphone"),
                @Mapping(target = "first.signDate", source = "indexPartatime"),
                @Mapping(target = "first.confirm", source = "indexPartaconfirmer"),
                @Mapping(target = "first.confirmTime", source = "indexPartaconfirmtime"),

                @Mapping(target = "second.tenantId", source = "indexPartbcompanyid"),
                @Mapping(target = "second.name", source = "indexPartbcompanyname"),
                @Mapping(target = "second.address", source = "indexPartbaddr"),
                @Mapping(target = "second.contactName", source = "indexPartbcontact"),
                @Mapping(target = "second.contactMobile", source = "indexPartbphone"),
                @Mapping(target = "second.signDate", source = "indexPartbtime"),
                @Mapping(target = "second.confirm", source = "indexPartbconfirmer"),
                @Mapping(target = "second.confirmTime", source = "indexPartbconfirmtime"),
        })
        @BaseMapping
        Contract convert(ContractIndexDO source);

        @AfterMapping
        default void after(@MappingTarget Contract target, ContractIndexDO source) {
            target.setRaw(JsonUtils.withNullJson(source));
        }

        List<Contract> convert(List<ContractIndexDO> list);

        OrderAttach convertAttach(Attach source);

        List<OrderAttach> convertAttach(List<Attach> source);

    }

    public static List<Contract> convert(List<ContractIndexDO> creation) {
        return CONVERT.convert(creation);
    }

    public static Contract convert(ContractIndexDO source) {
        return CONVERT.convert(source);
    }

    public static OrderAttach convertAttach(Attach source) {
        return CONVERT.convertAttach(source);
    }

    public static List<OrderAttach> convertAttach(List<Attach> source) {
        return CONVERT.convertAttach(source);
    }
}
